﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

namespace WEALTH.Manager.User
{
    public partial class EditUser : BasePage
    {
        private UserFilter userFilter = new UserFilter();
        private ExcuteDao userManage = new ExcuteDao();

        /// <summary>
        /// 页面初始化加载
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                this.btnSubmit.Visible = false;
                if (!string.IsNullOrEmpty(Request.QueryString["UserID"]))
                {
                    Session["UrlType"] = "EditUser";
                    Session["Edit_UserID"] = SpecialCharHandler.HtmlDiscode(Request.QueryString["UserID"]).Replace("&amp;", "&");

                    this.btnSubmit.Visible = true;
                    SetInitData();
                }
            }
        }

        /// <summary>
        /// 设置初始化页面信息
        /// </summary>
        private void SetInitData()
        {
            //获取用户信息
            DataTable dtuser = userManage.Get_UserByID(SpecialCharHandler.HtmlEncode(Request.QueryString["UserID"]).Replace("amp;", "&"));
            if (dtuser.Rows.Count > 0)
            {
                //获取用户角色
                DataTable dtrole = userManage.Get_UserRole(SpecialCharHandler.HtmlEncode(Request.QueryString["UserID"]).Replace("&amp;", "&"));

                Session["dtrole"] = dtrole;

                txtUserId.Text = SpecialCharHandler.HtmlDiscode(dtuser.Rows[0]["User_ID"].ToString());
                txtUserName.Text = SpecialCharHandler.HtmlDiscode(dtuser.Rows[0]["User_Name"].ToString());
                txtTel.Text = SpecialCharHandler.HtmlDiscode(dtuser.Rows[0]["User_Phone"].ToString());
                txtEmail.Text = SpecialCharHandler.HtmlDiscode(dtuser.Rows[0]["User_Email"].ToString());
                ddlStatus.SelectedValue = dtuser.Rows[0]["User_status"].ToString();
            }
            //userFilter.User_pwd = dtuser.Rows[0]["User_Pwd"].ToString();
        }

        /// <summary>
        /// 编辑按钮
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            try
            {
                //判断是否保存成功
                bool isOk = false;
                //验证页面数据
                if (!CheckData())
                {
                    return;
                }
                //设置需要添加的数据
                SetData();
                //添加用户信息到数据库中
                if (userManage.Update_User(userFilter) == -1)
                {
                    isOk = true;
                    //删除用户所有角色
                    if (userManage.Delete_UserRole(userFilter.User_ID) == -1)
                    {
                        foreach (CheckBoxList cbl in cblRole.Controls)
                        {
                            foreach (ListItem ckRole in cbl.Items)
                            {
                                if (ckRole.Selected)
                                {
                                    //加载用户角色
                                    if (userManage.Insert_UserRole(userFilter.User_ID, ckRole.Value) > -1)
                                    {
                                        isOk = false;
                                    }
                                }
                            }
                        }
                    }
                }

                if (isOk)
                {
                    //ShowMsg("编辑用户成功！", 0);
                    // Response.Write("<script>top.$('#loading').show();location.replace('UserList.aspx?msg=编辑用户成功！');</script>");
                    ReturnList("UserList.aspx", "编辑用户成功！");
                }
                else
                {
                    ShowMsg("编辑用户失败，请确认！", 1);
                }
            }
            catch (Exception ex)
            {
                ShowMsg(ex.Message.ToString(), 1);
            }
        }

        /// <summary>
        /// 验证页面内容
        /// </summary>
        /// <returns></returns>
        private bool CheckData()
        {
            //if (!string.IsNullOrEmpty(txtPassword.Text.TrimEnd())
            //    || !string.IsNullOrEmpty(txtPasswordConfirm.Text.TrimEnd()))
            //{
            //    if (!txtPassword.Text.Equals(txtPasswordConfirm.Text.TrimEnd()))
            //    {
            //        ShowMsg("密码两次输入的不一样！",1);
            //        txtPasswordConfirm.Focus();
            //        return false;
            //    }
            //}

            if (string.IsNullOrEmpty(txtUserName.Text.TrimEnd()))
            {
                ShowMsg("请输入真实姓名！", 1);
                txtUserName.Focus();
                return false;
            }

            if (string.IsNullOrEmpty(txtEmail.Text.TrimEnd()))
            {
                ShowMsg("请输入Email！", 1);
                txtEmail.Focus();
                return false;
            }
            #region 用户角色选择判断
            bool ischk = false;
            foreach (CheckBoxList cbl in cblRole.Controls)
            {
                foreach (ListItem ckRole in cbl.Items)
                {
                    if (ckRole.Selected)
                    {
                        ischk = true;
                    }
                }
            }
            if (!ischk)
            {
                ShowMsg("请选择用户角色！", 1);
                return false;
            }
            #endregion

            return true;
        }

        #region 设置页面数据到Filter
        /// <summary>
        /// 设置页面数据到Filter
        /// </summary>
        private void SetData()
        {
            userFilter.User_ID = SpecialCharHandler.HtmlEncode(txtUserId.Text.TrimEnd());
            userFilter.User_Name = SpecialCharHandler.HtmlEncode(txtUserName.Text.TrimEnd());
            ////如果密码没输入则密码不变
            //if (!string.IsNullOrEmpty(txtPassword.Text))
            //{
            //    userFilter.User_pwd = txtPassword.Text.TrimEnd();
            //}
            userFilter.User_Tel = SpecialCharHandler.HtmlEncode(txtTel.Text.TrimEnd());
            userFilter.User_Mail = SpecialCharHandler.HtmlEncode(txtEmail.Text.TrimEnd());
            userFilter.User_status = ddlStatus.SelectedValue;
        }
        #endregion

        /// <summary>
        ///  密码重置
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnPassword_Click(object sender, EventArgs e)
        {
            userFilter = new UserFilter();
            userFilter.User_ID = SpecialCharHandler.HtmlEncode(txtUserId.Text.TrimEnd());
            userFilter.User_pwd = AppFunction.Encrypt(SpecialCharHandler.HtmlEncode(txtUserId.Text.TrimEnd()));
            userFilter.User_Name = SpecialCharHandler.HtmlEncode(txtUserName.Text.TrimEnd());
            userFilter.User_Mail = SpecialCharHandler.HtmlEncode(txtEmail.Text.TrimEnd());

            int isok = 0;
            isok = userManage.Update_User_Pwd(userFilter);
            if (isok == -1)
            {
                ShowMsg("密码重置成功", 0);
            }
            else
            {
                ShowMsg("密码重置失败", 1);
            }

        }
    }
}